package com.ck.mapstruct.mapper;

import com.alibaba.fastjson.JSON;
import com.ck.mapstruct.dto.CarDto;
import com.ck.mapstruct.entity.Car;
import com.ck.mapstruct.entity.CarConfig;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import java.util.List;


@Mapper(componentModel = "spring")
public interface CarMapper {
    /**
     * 1.类型相同(String), 字段不相同
     * 2.类型不同(源Date,目标String), 字段相同
     * 3.类型相同, 字段相同, 目标不需要值
     * @param car
     * @return
     */
    @Mappings({
            @Mapping(source = "numberOfSeats", target = "seatCount"),
            @Mapping(target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss"),
            @Mapping(target = "password", ignore = true)
    })
    CarDto carToCarDto(Car car);

    /**
     * 自定义方法: JSON字符串转实体类list集合
     * @param config
     * @return
     */
    default List<CarConfig> strConfigToListCarConfig(String config) {
        return JSON.parseArray(config, CarConfig.class);
    }

    /**
     * 自定义方法: 实体类list集合转JSON字符串
     * @param list
     * @return
     */
    default String listCarConfigToStrConfig(List<CarConfig> list) {
        return JSON.toJSONString(list);
    }

}
